package com.ruifu.proj.controller;


import com.fhzncloud.cloud.common.core.util.R;
import com.fhzncloud.cloud.common.security.service.CloudUser;
import com.fhzncloud.cloud.common.security.util.SecurityUtils;
import com.google.common.collect.Lists;
import com.ruifu.common.po.PmPtObject;
import com.ruifu.common.vo.ResultVO;
import com.ruifu.proj.dto.ptobject.PmPtCopyInsertObjectDTO;
import com.ruifu.proj.dto.ptobject.PmPtInsertObjectDTO;
import com.ruifu.proj.service.PmPtObjectService;
import com.ruifu.proj.vo.pmPtObject.PmPtObjectDocVerVO;
import com.ruifu.proj.vo.pmPtObject.PmPtObjectResultVO;
import lombok.extern.log4j.Log4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 项目、任务关联数据对象
 *
 * @author nyr
 */
@RestController
@CrossOrigin
@RequestMapping("/pmPtObject")
public class PmPtObjectController {

    @Autowired
    private PmPtObjectService pmPtObjectService;

    /**
     * 用于存储登陆人信息
     */
    private CloudUser cloudUser;

    /**
     * 通过url请求方法之前,调用该方法获取登陆人信息
     */
    @ModelAttribute
    public void before() {
        cloudUser = SecurityUtils.getUser();
    }

    /**
     * 添加输入(输出)文档类型和输入(输出)文档
     *
     * @param pmPtInsertObjectDTO
     * @return
     */
    @PostMapping(value = "/insert")
    public ResultVO insert(@RequestBody PmPtInsertObjectDTO pmPtInsertObjectDTO) {
        try {
            pmPtObjectService.insert(pmPtInsertObjectDTO, cloudUser);
            return new ResultVO(true, "输入(输出)新建成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, "输入(输出)新建失败");
        }
    }

    /**
     * 添加输入(输出)文档类型和输入(输出)文档
     *
     * @param pmPtInsertObjectDTOList
     * @return
     */
    @PostMapping(value = "/insertPL")
    public ResultVO insertPL(@RequestBody List<PmPtInsertObjectDTO> pmPtInsertObjectDTOList) {
        try {
            pmPtObjectService.insertPL(pmPtInsertObjectDTOList, cloudUser);
            return new ResultVO(true, "输入(输出)新建成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, "输入(输出)新建失败");
        }
    }

    /**
     * 批量删除输入(输出)文档类型和输入(输出)文档
     *
     * @param poIds
     * @return
     */
    @PostMapping(value = "/delete")
    public ResultVO delete(@RequestBody Long[] poIds) {
        try {
            pmPtObjectService.delete(poIds, cloudUser);
            return new ResultVO(true, "输入(输出)删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, "输入(输出)删除失败");
        }
    }


    /**
     * 根据选择的任务来源名称查询该任务的输出文档作为该任务的输入文档
     *
     * @param pmPtCopyInsertObjectDTO
     * @return
     */
    @PostMapping("/insertCopyByPtCode")
    public ResultVO insertCopyByPtCode(@RequestBody PmPtCopyInsertObjectDTO pmPtCopyInsertObjectDTO) {
        try {
            pmPtObjectService.insertCopyByPtCode(pmPtCopyInsertObjectDTO, cloudUser);
            return new ResultVO(true, "插入任务来源输出文档成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, "插入任务来源输出文档失败");
        }
    }

    /**
     * 查询I/O文档页
     *
     * @param ptId
     * @param poIOType
     * @return
     */
    @GetMapping(value = "/findByPtIdAndPoIOType/{ptId}/{poIOType}")
    public PmPtObjectResultVO findByPtIdAndPoIOType(@PathVariable("ptId") Long ptId,
                                                    @PathVariable("poIOType") String poIOType) {
        return pmPtObjectService.findByPoPtIdAndPoIOType(ptId, poIOType, cloudUser);
    }

    /**
     * 2020.5.27 查询任务对应的输出文档  同时需要符合对应的输入类型
     * @param ptId
     * @param poIOType
     * @return
     */
    @GetMapping(value = "/findObjectByPtId/{ptId}/{poIOType}/{poObjTypeId}")
    public R<List<PmPtObjectDocVerVO>> findObjectByPtId(@PathVariable("ptId") Long ptId,
                              @PathVariable("poIOType") String poIOType,
                              @PathVariable("poObjTypeId")Long poObjTypeId) {

        if (ptId==null || poObjTypeId==null || StringUtils.isBlank(poIOType)){
            return R.failed("参数异常");
        }
        try {
            return R.ok(pmPtObjectService.findObjectByPtId(ptId, poIOType,poObjTypeId, cloudUser));
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed(e.getMessage());
        }

    }

    /**
     * 修改输出文档类型是否必须
     *
     * @param poId   I/O记录id
     * @param isMust 是否必须
     * @return
     */
    @GetMapping(value = "/updatePoIsMustByPoId/{poId}/{isMust}")
    public ResultVO updatePoIsMustByPoId(@PathVariable Long poId,
                                         @PathVariable Boolean isMust) {
        try {
            pmPtObjectService.updatePoIsMustByPoId(poId, isMust, cloudUser);
            return new ResultVO(true, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, e.getMessage());
        }
    }
}
